package com.example.shiro.controller;

import com.example.shiro.domain.User;
import com.example.shiro.dto.AddUserDto;
import com.example.shiro.dto.UpdateUserDto;
import com.example.shiro.response.ResponseMsg;
import com.example.shiro.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：jujh
 * @ Date       ：Created in 10:26 2018/10/31
 * @ Description：
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //用户信息管理
    @RequestMapping(value = "/user/go-user-page")
    public String stuinfo(HttpSession session) {
        return "user-list";
    }


    //加载用户数据
    @RequestMapping(value = "/user/user-list")
    @ResponseBody
    public Map queryUserList() {
        List<User> userList = userService.findAll();
        Map resultMap = new HashMap();
        resultMap.put("total", 1);
        resultMap.put("rows", userList);
        return resultMap;
    }

    //新增用户数据
    @RequestMapping(value = "/user/add")
    @ResponseBody
    public ResponseMsg addUser(AddUserDto addUserDto) {
        userService.addUser(addUserDto);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setStatus(ResponseMsg.STATUS_SUCCESS);
        return responseMsg;
    }

    //修改用户数据
    @RequestMapping(value = "/user/update")
    @ResponseBody
    public ResponseMsg updateUser(UpdateUserDto updateUserDto) {
        userService.updateUser(updateUserDto);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setStatus(ResponseMsg.STATUS_SUCCESS);
        return responseMsg;
    }

    //删除用户数据
    @RequestMapping(value = "/user/delete")
    @ResponseBody
    public ResponseMsg deleteUser(String id) {
        userService.deleteUser(id);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setStatus(ResponseMsg.STATUS_SUCCESS);
        return responseMsg;
    }
}
